*******************************************

*			GENERATE CIRCLE               *
*					AND DOUGHNUT		  *
*			             DATA SETS		  *
*******************************************
clear
clear matrix
clear mata
set maxvar 10000
set more off
cd "C:\Users\maxha\Box\EDWIN_DEP_Well_Data\Washington_County_Project\Final Code\Circle and Doughnut Tables" /*Home PC*/ 

use "C:\Users\maxha\Box\EDWIN_DEP_Well_Data\Washington_County_Project\Final Code\Compiling Near Table\WC_Wells_Soil_Near_Withdata_Fields_Pools.dta" /*Home PC*/ 

preserve /*Tab of our sample wells by minyear*/
	drop if minyear>=1970
	drop if minyear==1815
	tabstat minyear, by (welltype) stat(mean p50)
restore

preserve /*Tab of our sample wells operators*/
	drop if minyear>=1970
	tab operatorname if welltype=="OIL" 
	tab operatorname if welltype=="GAS"
restore

***GENERATE VARIABLES***

*WELL UNPLUGGED
	replace plugged = 2 if plugged == 1
	replace plugged = 1 if plugged == 0
	replace plugged = 0 if plugged == 2
	rename plugged unplugged
	
*UN/PLUGGED OVER PERIOD
	replace date_plugg_year=1 if unplugged==0 & date_plugg_year==.
	gen bplugged1 = (date_plugg_year<1970 & unplugged==0) /*1= plugged for entire period*/
	gen bunplugged2005 = ((date_plugg_year>2005 & unplugged==0)|unplugged==1) /*1= unplugged for entire period*/
	gen bunplugged2017 = ((date_plugg_year>2017 & unplugged==0)|unplugged==1) /*1= unplugged for entire period*/

*WELL TYPE BINARIES
	gen oil_type= (welltype=="OIL")
	gen gas_type= (welltype=="GAS")
	gen oil2_type= (welltype=="OIL AND GAS")
	
*SOIL TYPE BINARIES
	gen soil_WB_notlim= (dwelling_with_basement=="Not limited")
	gen soil_WB_somlim= (dwelling_with_basement=="Somewhat limited")
	gen soil_WB_vrylim= (dwelling_with_basement=="Very limited")
	gen soil_WOB_notlim= (dwelling_without_basement=="Not limited")
	gen soil_WOB_somlim= (dwelling_without_basement=="Somewhat limited")
	gen soil_WOB_vrylim= (dwelling_without_basement=="Very limited") /*"Not rated" omitted for both*/
	
*NEAREST FIELDS
	egen Nearest_Park_Gameland= rowmin(Nearest_Pa Nearest_Ga)
	
*CREATE DUPLICATES
	expand 5

*CIRCLE/DOUGHNUT/NEAR_FAR MACROS AND SCALARS
	/**Turns on circle globals, must turn off either doughnut table or near_far table
	global d_c= "circle_"
	global low_high1 = "0_50"
	global low_high2 = "_0_100"
	global low_high3 = "0_150"
	global low_high4 = "0_200"
	global low_high5 = "0_250"
	scalar low1 = 0
	scalar low2 = 0
	scalar low3 = 0
	scalar low4 = 0
	scalar low5 = 0
	scalar high1 = 50
	scalar high2 = 100
	scalar high3 = 150
	scalar high4 = 200
	scalar high5 = 250*/
	
	*Turns on doughnut globals, must turn off either circle table or near_far table
	global d_c= "doughnut_"
	global low_high1 = "0_50"
	global low_high2 = "_50_100"
	global low_high3 = "100_150"
	global low_high4 = "150_200"
	global low_high5 = "200_250"
	scalar low1 = 0
	scalar low2 = 50
	scalar low3 = 100
	scalar low4 = 150
	scalar low5 = 200
	scalar high1 = 50
	scalar high2 = 100
	scalar high3 = 150
	scalar high4 = 200
	scalar high5 = 250
	
	/*Turns on near_far globals, must turn off either circle table or doughnut table
	global d_c= "near_"
	global low_high1 = "0_50"
	global low_high2 = "_50_250"
	global low_high3 = "invalid_1"
	global low_high4 = "invalid_2"
	global low_high5 = "invalid_3"
	scalar low1 = 0
	scalar low2 = 50
	scalar low3 = 0
	scalar low4 = 0
	scalar low5 = 0
	scalar high1 = 50
	scalar high2 = 250
	scalar high3 = 0
	scalar high4 = 0
	scalar high5 = 0*/
	
	/**Turns on near_far globals, must turn off either circle table or doughnut table
	global d_c= "near_new_"
	global low_high1 = "0_100"
	global low_high2 = "_200_250"
	global low_high3 = "invalid_1"
	global low_high4 = "invalid_2"
	global low_high5 = "invalid_3"
	scalar low1 = 0
	scalar low2 = 200
	scalar low3 = 0
	scalar low4 = 0
	scalar low5 = 0
	scalar high1 = 100
	scalar high2 = 250
	scalar high3 = 0
	scalar high4 = 0
	scalar high5 = 0*/
	
	/**Turns on near_far globals, must turn off either circle table or doughnut table
	global d_c= "near_new_"
	global low_high1 = "0_50"
	global low_high2 = "_200_250"
	global low_high3 = "invalid_1"
	global low_high4 = "invalid_2"
	global low_high5 = "invalid_3"
	scalar low1 = 0
	scalar low2 = 200
	scalar low3 = 0
	scalar low4 = 0
	scalar low5 = 0
	scalar high1 = 50
	scalar high2 = 250
	scalar high3 = 0
	scalar high4 = 0
	scalar high5 = 0*/
	
*GENERATE CIRCLE/DOUGHNUT CIRCLE CATEGORICAL VARIALBE AND BINARIES
	sort well_id
	quietly by well_id: gen bldg_$d_c = cond(_N==1,0,_n)
	gen $d_c$low_high1 = (bldg_$d_c ==1)
	gen $d_c$low_high2 = (bldg_$d_c ==2)
	gen $d_c$low_high3 = (bldg_$d_c ==3)
	gen $d_c$low_high4 = (bldg_$d_c ==4)
	gen $d_c$low_high5 = (bldg_$d_c ==5)

*Full Count 1970-1975 BUILDINGS WITHIN 250 METER	
	foreach x of numlist 1/413 {
		foreach y of numlist 250 {
		gen post_build_within_`y'_`x'= (near_building_dist`x'<=`y'& YRBLT`x'>=1970 & YRBLT`x'<=1975) /*Looking for building between 1970 to 1975*/
		}
	}

	global post_vars250 post_build_within_250_*

	egen within_250_70_75_build=rowtotal($post_vars250)
	
	drop $post_vars250
	
*******PRE VARS******
foreach s in 0 1920 { /*START DATES*/
	*PRE 1970 BUILDINGS WITHIN 250 METER	
	foreach x of numlist 1/413 {
		foreach y of numlist 250 {
		gen pre_build_within_`y'_`x'= (near_building_dist`x'<=`y'& YRBLT`x'>=`s' & YRBLT`x'<1970)
		}
	}

	global pre_vars250 pre_build_within_250_*

	egen pre_win_250_bld_`s'_1970=rowtotal($pre_vars250)
	
	drop $pre_vars250
	
	*UNPLUGGED PRE 1970 WELLS WITHIN 50 to 250 METER	

	foreach x of numlist 1/16 {
		foreach y of numlist 50 {
		gen pre_well_within_`y'_`x'=(near_well_dist`x'<=250 & near_well_dist`x'>=50 & minyear`x'<1970 & minyear`x'>=`s' & plugged`x' == 0)
		}
	}

	global pre_wvars50 pre_well_within_50_*
	

	egen unplug_pre_well_win_50_250_`s'=rowtotal($pre_wvars50)
	
	drop $pre_wvars50
	
	*UNPLUGGED PRE 1970 WELLS WITHIN 250 METER	

	foreach x of numlist 1/16 {
		foreach y of numlist 250 {
		gen pre_well_within_`y'_`x'=(near_well_dist`x'<=`y' & minyear`x'>=`s' & minyear`x'<1970 & plugged`x' == 0)
		}
	}

	global pre_wvars250 pre_well_within_250_*
	

	egen unplug_pre_well_win_250_`s'=rowtotal($pre_wvars250)
	
	drop $pre_wvars250
	
	*PLUGGED PRE 1970 WELLS WITHIN 250 METER	

	foreach x of numlist 1/16 {
		foreach y of numlist 250 {
		gen pre_well_within_`y'_`x'=(near_well_dist`x'<=`y' & minyear`x'>=`s' & minyear`x'<1970 & plugged`x' == 1)
		}
	}

	global pre_wvars250 pre_well_within_250_*
	

	egen plug_pre_well_win_250_`s'=rowtotal($pre_wvars250)
	
	drop $pre_wvars250
	
	*PRE 1970 BUILDINGS WITHIN CIRCLE/DOUGHNUT

	foreach x of numlist 1/413 {
		gen pre_build_0_50_`x'= (near_building_dist`x'>= low1 & near_building_dist`x'<high1 & YRBLT`x'>=`s' & YRBLT`x'<1970 & YRBLT`x'!=.)
		gen pre_build_0_100_`x'= (near_building_dist`x'>= low2 & near_building_dist`x'<high2 & YRBLT`x'>=`s' & YRBLT`x'<1970 & YRBLT`x'!=.)
		gen pre_build_0_150_`x'= (near_building_dist`x'>= low3 & near_building_dist`x'<high3 & YRBLT`x'>=`s' & YRBLT`x'<1970 & YRBLT`x'!=.)
		gen pre_build_0_200_`x'= (near_building_dist`x'>= low4  & near_building_dist`x'<high4 & YRBLT`x'>=`s' & YRBLT`x'<1970 & YRBLT`x'!=.)
		gen pre_build_0_250_`x'= (near_building_dist`x'>= low5  & near_building_dist`x'<high5 & YRBLT`x'>=`s' & YRBLT`x'<1970 & YRBLT`x'!=.)
		}

	global pre_vars_0to50 pre_build_0_50_*
	global pre_vars_0to100 pre_build_0_100_*
	global pre_vars_0to150 pre_build_0_150_*
	global pre_vars_0to200 pre_build_0_200_*
	global pre_vars_0to250 pre_build_0_250_*

	egen pre_build_0to50=rowtotal($pre_vars_0to50)
	egen pre_build_0to100=rowtotal($pre_vars_0to100)
	egen pre_build_0to150=rowtotal($pre_vars_0to150)
	egen pre_build_0to200=rowtotal($pre_vars_0to200)
	egen pre_build_0to250=rowtotal($pre_vars_0to250)
	
	gen pre_build_y`s'=pre_build_0to50 if bldg_$d_c ==1 
	replace pre_build_y`s'=pre_build_0to100 if bldg_$d_c ==2
	replace pre_build_y`s'=pre_build_0to150 if bldg_$d_c ==3
	replace pre_build_y`s'=pre_build_0to200 if bldg_$d_c ==4
	replace pre_build_y`s'=pre_build_0to250 if bldg_$d_c ==5
	
	drop $pre_vars_0to50 $pre_vars_0to100 $pre_vars_0to150 $pre_vars_0to200 $pre_vars_0to250 pre_build_0*
	
		*PRE 1970 COMMERCIAL BUILDINGS WITHIN CIRCLE/DOUGHNUT

	foreach x of numlist 1/413 {
		gen pre_build_0_50_`x'= (near_building_dist`x'>= low1 & near_building_dist`x'<high1 & YRBLT`x'>=`s' & YRBLT`x'<1970 & YRBLT`x'!=. & near_building_type`x'=="commercial")
		gen pre_build_0_100_`x'= (near_building_dist`x'>= low2 & near_building_dist`x'<high2 & YRBLT`x'>=`s' & YRBLT`x'<1970 & YRBLT`x'!=. & near_building_type`x'=="commercial")
		gen pre_build_0_150_`x'= (near_building_dist`x'>= low3 & near_building_dist`x'<high3 & YRBLT`x'>=`s' & YRBLT`x'<1970 & YRBLT`x'!=. & near_building_type`x'=="commercial")
		gen pre_build_0_200_`x'= (near_building_dist`x'>= low4  & near_building_dist`x'<high4 & YRBLT`x'>=`s' & YRBLT`x'<1970 & YRBLT`x'!=. & near_building_type`x'=="commercial")
		gen pre_build_0_250_`x'= (near_building_dist`x'>= low5  & near_building_dist`x'<high5 & YRBLT`x'>=`s' & YRBLT`x'<1970 & YRBLT`x'!=. & near_building_type`x'=="commercial")
		}

	global pre_vars_0to50 pre_build_0_50_*
	global pre_vars_0to100 pre_build_0_100_*
	global pre_vars_0to150 pre_build_0_150_*
	global pre_vars_0to200 pre_build_0_200_*
	global pre_vars_0to250 pre_build_0_250_*

	egen pre_build_0to50=rowtotal($pre_vars_0to50)
	egen pre_build_0to100=rowtotal($pre_vars_0to100)
	egen pre_build_0to150=rowtotal($pre_vars_0to150)
	egen pre_build_0to200=rowtotal($pre_vars_0to200)
	egen pre_build_0to250=rowtotal($pre_vars_0to250)
	
	gen pre_com_build_y`s'=pre_build_0to50 if bldg_$d_c ==1 
	replace pre_com_build_y`s'=pre_build_0to100 if bldg_$d_c ==2
	replace pre_com_build_y`s'=pre_build_0to150 if bldg_$d_c ==3
	replace pre_com_build_y`s'=pre_build_0to200 if bldg_$d_c ==4
	replace pre_com_build_y`s'=pre_build_0to250 if bldg_$d_c ==5
	
	drop $pre_vars_0to50 $pre_vars_0to100 $pre_vars_0to150 $pre_vars_0to200 $pre_vars_0to250 pre_build_0*
	
			*PRE 1970 COMMERCIAL BUILDINGS WITHIN CIRCLE/DOUGHNUT

	foreach x of numlist 1/413 {
		gen pre_build_0_50_`x'= (near_building_dist`x'>= low1 & near_building_dist`x'<high1 & YRBLT`x'>=`s' & YRBLT`x'<1970 & YRBLT`x'!=. & near_building_type`x'=="out")
		gen pre_build_0_100_`x'= (near_building_dist`x'>= low2 & near_building_dist`x'<high2 & YRBLT`x'>=`s' & YRBLT`x'<1970 & YRBLT`x'!=. & near_building_type`x'=="out")
		gen pre_build_0_150_`x'= (near_building_dist`x'>= low3 & near_building_dist`x'<high3 & YRBLT`x'>=`s' & YRBLT`x'<1970 & YRBLT`x'!=. & near_building_type`x'=="out")
		gen pre_build_0_200_`x'= (near_building_dist`x'>= low4  & near_building_dist`x'<high4 & YRBLT`x'>=`s' & YRBLT`x'<1970 & YRBLT`x'!=. & near_building_type`x'=="out")
		gen pre_build_0_250_`x'= (near_building_dist`x'>= low5  & near_building_dist`x'<high5 & YRBLT`x'>=`s' & YRBLT`x'<1970 & YRBLT`x'!=. & near_building_type`x'=="out")
		}

	global pre_vars_0to50 pre_build_0_50_*
	global pre_vars_0to100 pre_build_0_100_*
	global pre_vars_0to150 pre_build_0_150_*
	global pre_vars_0to200 pre_build_0_200_*
	global pre_vars_0to250 pre_build_0_250_*

	egen pre_build_0to50=rowtotal($pre_vars_0to50)
	egen pre_build_0to100=rowtotal($pre_vars_0to100)
	egen pre_build_0to150=rowtotal($pre_vars_0to150)
	egen pre_build_0to200=rowtotal($pre_vars_0to200)
	egen pre_build_0to250=rowtotal($pre_vars_0to250)
	
	gen pre_out_build_y`s'=pre_build_0to50 if bldg_$d_c ==1 
	replace pre_out_build_y`s'=pre_build_0to100 if bldg_$d_c ==2
	replace pre_out_build_y`s'=pre_build_0to150 if bldg_$d_c ==3
	replace pre_out_build_y`s'=pre_build_0to200 if bldg_$d_c ==4
	replace pre_out_build_y`s'=pre_build_0to250 if bldg_$d_c ==5
	
	drop $pre_vars_0to50 $pre_vars_0to100 $pre_vars_0to150 $pre_vars_0to200 $pre_vars_0to250 pre_build_0*
	
	*PRE 1970 RESIDENTIAL BUILDINGS WITHIN CIRCLE/DOUGHNUT

	foreach x of numlist 1/413 {
		gen pre_build_0_50_`x'= (near_building_dist`x'>= low1 & near_building_dist`x'<high1 & YRBLT`x'>=`s' & YRBLT`x'<1970 & YRBLT`x'!=. & near_building_type`x'=="main")
		gen pre_build_0_100_`x'= (near_building_dist`x'>= low2 & near_building_dist`x'<high2 & YRBLT`x'>=`s' & YRBLT`x'<1970 & YRBLT`x'!=. & near_building_type`x'=="main")
		gen pre_build_0_150_`x'= (near_building_dist`x'>= low3 & near_building_dist`x'<high3 & YRBLT`x'>=`s' & YRBLT`x'<1970 & YRBLT`x'!=. & near_building_type`x'=="main")
		gen pre_build_0_200_`x'= (near_building_dist`x'>= low4  & near_building_dist`x'<high4 & YRBLT`x'>=`s' & YRBLT`x'<1970 & YRBLT`x'!=. & near_building_type`x'=="main")
		gen pre_build_0_250_`x'= (near_building_dist`x'>= low5  & near_building_dist`x'<high5 & YRBLT`x'>=`s' & YRBLT`x'<1970 & YRBLT`x'!=. & near_building_type`x'=="main")
		}

	global pre_vars_0to50 pre_build_0_50_*
	global pre_vars_0to100 pre_build_0_100_*
	global pre_vars_0to150 pre_build_0_150_*
	global pre_vars_0to200 pre_build_0_200_*
	global pre_vars_0to250 pre_build_0_250_*

	egen pre_build_0to50=rowtotal($pre_vars_0to50)
	egen pre_build_0to100=rowtotal($pre_vars_0to100)
	egen pre_build_0to150=rowtotal($pre_vars_0to150)
	egen pre_build_0to200=rowtotal($pre_vars_0to200)
	egen pre_build_0to250=rowtotal($pre_vars_0to250)
	
	gen pre_res_build_y`s'=pre_build_0to50 if bldg_$d_c ==1 
	replace pre_res_build_y`s'=pre_build_0to100 if bldg_$d_c ==2
	replace pre_res_build_y`s'=pre_build_0to150 if bldg_$d_c ==3
	replace pre_res_build_y`s'=pre_build_0to200 if bldg_$d_c ==4
	replace pre_res_build_y`s'=pre_build_0to250 if bldg_$d_c ==5
	
	drop $pre_vars_0to50 $pre_vars_0to100 $pre_vars_0to150 $pre_vars_0to200 $pre_vars_0to250 pre_build_0*
	
	* Total Rooms in Pre-1970 Residential Buildings* WITHIN CIRCLE/DOUGHNUT

	foreach x of numlist 1/413 {
		gen pre_RMTOT_0_50_`x'= RMTOT`x' if near_building_dist`x'>= low1 & near_building_dist`x' <high1 & YRBLT`x'>=`s' &  YRBLT`x'<1970 & YRBLT`x'!=.
		gen pre_RMTOT_0_100_`x'= RMTOT`x' if near_building_dist`x'>= low2 & near_building_dist`x' <high2 & YRBLT`x'>=`s' &  YRBLT`x'<1970 & YRBLT`x'!=.
		gen pre_RMTOT_0_150_`x'= RMTOT`x' if near_building_dist`x'>= low3 & near_building_dist`x' <high3 & YRBLT`x'>=`s' &  YRBLT`x'<1970 & YRBLT`x'!=.
		gen pre_RMTOT_0_200_`x'= RMTOT`x' if near_building_dist`x'>= low4 & near_building_dist`x' <high4 & YRBLT`x'>=`s' &  YRBLT`x'<1970 & YRBLT`x'!=.
		gen pre_RMTOT_0_250_`x'= RMTOT`x' if near_building_dist`x'>= low5 & near_building_dist`x' <high5 & YRBLT`x'>=`s' &  YRBLT`x'<1970 & YRBLT`x'!=.
		}

	global pre_vars_0to50 pre_RMTOT_0_50_*
	global pre_vars_0to100 pre_RMTOT_0_100_*
	global pre_vars_0to150 pre_RMTOT_0_150_*
	global pre_vars_0to200 pre_RMTOT_0_200_*
	global pre_vars_0to250 pre_RMTOT_0_250_*

	egen pre_RMTOT_0to50=rowtotal($pre_vars_0to50)
	egen pre_RMTOT_0to100=rowtotal($pre_vars_0to100)
	egen pre_RMTOT_0to150=rowtotal($pre_vars_0to150)
	egen pre_RMTOT_0to200=rowtotal($pre_vars_0to200)
	egen pre_RMTOT_0to250=rowtotal($pre_vars_0to250)
	
	gen pre_RMTOT_y`s'=pre_RMTOT_0to50 if bldg_$d_c ==1
	replace pre_RMTOT_y`s'=pre_RMTOT_0to100 if bldg_$d_c ==2
	replace pre_RMTOT_y`s'=pre_RMTOT_0to150 if bldg_$d_c ==3
	replace pre_RMTOT_y`s'=pre_RMTOT_0to200 if bldg_$d_c ==4
	replace pre_RMTOT_y`s'=pre_RMTOT_0to250 if bldg_$d_c ==5
	
	drop $pre_vars_0to50 $pre_vars_0to100 $pre_vars_0to150 $pre_vars_0to200 $pre_vars_0to250 pre_RMTOT_0*
	
	* Total SFLA in Pre-1970 Residential Buildings* WITHIN CIRCLE/DOUGHNUT

	foreach x of numlist 1/413 {
		gen pre_SFLA_0_50_`x'= SFLA`x' if near_building_dist`x'>= low1 & near_building_dist`x'<high1 &  YRBLT`x'<1970 & YRBLT`x'!=.
		gen pre_SFLA_0_100_`x'= SFLA`x' if near_building_dist`x'>= low2 & near_building_dist`x'<high2 &  YRBLT`x'<1970 & YRBLT`x'!=.
		gen pre_SFLA_0_150_`x'= SFLA`x' if near_building_dist`x'>= low3 & near_building_dist`x'<high3 &  YRBLT`x'<1970 & YRBLT`x'!=.
		gen pre_SFLA_0_200_`x'= SFLA`x' if near_building_dist`x'>= low4 & near_building_dist`x'<high4 &  YRBLT`x'<1970 & YRBLT`x'!=.
		gen pre_SFLA_0_250_`x'= SFLA`x' if near_building_dist`x'>= low5 & near_building_dist`x'<high5 &  YRBLT`x'<1970 & YRBLT`x'!=.
		}

	global pre_vars_0to50 pre_SFLA_0_50_*
	global pre_vars_0to100 pre_SFLA_0_100_*
	global pre_vars_0to150 pre_SFLA_0_150_*
	global pre_vars_0to200 pre_SFLA_0_200_*
	global pre_vars_0to250 pre_SFLA_0_250_*

	egen pre_SFLA_0to50=rowtotal($pre_vars_0to50)
	egen pre_SFLA_0to100=rowtotal($pre_vars_0to100)
	egen pre_SFLA_0to150=rowtotal($pre_vars_0to150)
	egen pre_SFLA_0to200=rowtotal($pre_vars_0to200)
	egen pre_SFLA_0to250=rowtotal($pre_vars_0to250)
	
	gen pre_SFLA_y`s'=pre_SFLA_0to50 if bldg_$d_c ==1
	replace pre_SFLA_y`s'=pre_SFLA_0to100 if bldg_$d_c ==2
	replace pre_SFLA_y`s'=pre_SFLA_0to150 if bldg_$d_c ==3
	replace pre_SFLA_y`s'=pre_SFLA_0to200 if bldg_$d_c ==4
	replace pre_SFLA_y`s'=pre_SFLA_0to250 if bldg_$d_c ==5
	
	drop $pre_vars_0to50 $pre_vars_0to100 $pre_vars_0to150 $pre_vars_0to200 $pre_vars_0to250 pre_SFLA_0*
	
	* Square Foot Footprint of Pre-1970 Buildings* WITHIN CIRCLE/DOUGHNUT

	foreach x of numlist 1/413 {
		gen pre_SqM_FtPrnt_0_50_`x'= FootPrint_Area_SqM`x' if near_building_dist`x'>= low1 & near_building_dist`x' <high1 & YRBLT`x'>=`s' &  YRBLT`x'<1970 & YRBLT`x'!=.
		gen pre_SqM_FtPrnt_0_100_`x'= FootPrint_Area_SqM`x' if near_building_dist`x'>= low2 & near_building_dist`x' <high2 & YRBLT`x'>=`s' &  YRBLT`x'<1970 & YRBLT`x'!=.
		gen pre_SqM_FtPrnt_0_150_`x'= FootPrint_Area_SqM`x' if near_building_dist`x'>= low3 & near_building_dist`x' <high3 & YRBLT`x'>=`s' &  YRBLT`x'<1970 & YRBLT`x'!=.
		gen pre_SqM_FtPrnt_0_200_`x'= FootPrint_Area_SqM`x' if near_building_dist`x'>= low4 & near_building_dist`x' <high4 & YRBLT`x'>=`s' &  YRBLT`x'<1970 & YRBLT`x'!=.
		gen pre_SqM_FtPrnt_0_250_`x'= FootPrint_Area_SqM`x' if near_building_dist`x'>= low5 & near_building_dist`x' <high5 & YRBLT`x'>=`s' &  YRBLT`x'<1970 & YRBLT`x'!=.
		}

	global pre_vars_0to50 pre_SqM_FtPrnt_0_50_*
	global pre_vars_0to100 pre_SqM_FtPrnt_0_100_*
	global pre_vars_0to150 pre_SqM_FtPrnt_0_150_*
	global pre_vars_0to200 pre_SqM_FtPrnt_0_200_*
	global pre_vars_0to250 pre_SqM_FtPrnt_0_250_*

	egen pre_SqM_0to50=rowtotal($pre_vars_0to50)
	egen pre_SqM_0to100=rowtotal($pre_vars_0to100)
	egen pre_SqM_0to150=rowtotal($pre_vars_0to150)
	egen pre_SqM_0to200=rowtotal($pre_vars_0to200)
	egen pre_SqM_0to250=rowtotal($pre_vars_0to250)
	
	gen pre_SqM_y`s'=pre_SqM_0to50 if bldg_$d_c ==1
	replace pre_SqM_y`s'=pre_SqM_0to100 if bldg_$d_c ==2
	replace pre_SqM_y`s'=pre_SqM_0to150 if bldg_$d_c ==3
	replace pre_SqM_y`s'=pre_SqM_0to200 if bldg_$d_c ==4
	replace pre_SqM_y`s'=pre_SqM_0to250 if bldg_$d_c ==5
	
	drop $pre_vars_0to50 $pre_vars_0to100 $pre_vars_0to150 $pre_vars_0to200 $pre_vars_0to250 pre_SqM_0*
	
	* Square Foot Footprint residential of Pre-1970 Buildings* WITHIN CIRCLE/DOUGHNUT

	foreach x of numlist 1/413 {
		gen pre_SqM_FtPrnt_0_50_`x'= FootPrint_Area_SqM`x' if near_building_dist`x'>= low1 & near_building_dist`x' <high1 & YRBLT`x'>=`s' &  YRBLT`x'<1970 & YRBLT`x'!=. & near_building_type`x'=="main"
		gen pre_SqM_FtPrnt_0_100_`x'= FootPrint_Area_SqM`x' if near_building_dist`x'>= low2 & near_building_dist`x' <high2 & YRBLT`x'>=`s' &  YRBLT`x'<1970 & YRBLT`x'!=. & near_building_type`x'=="main"
		gen pre_SqM_FtPrnt_0_150_`x'= FootPrint_Area_SqM`x' if near_building_dist`x'>= low3 & near_building_dist`x' <high3 & YRBLT`x'>=`s' &  YRBLT`x'<1970 & YRBLT`x'!=. & near_building_type`x'=="main"
		gen pre_SqM_FtPrnt_0_200_`x'= FootPrint_Area_SqM`x' if near_building_dist`x'>= low4 & near_building_dist`x' <high4 & YRBLT`x'>=`s' &  YRBLT`x'<1970 & YRBLT`x'!=. & near_building_type`x'=="main"
		gen pre_SqM_FtPrnt_0_250_`x'= FootPrint_Area_SqM`x' if near_building_dist`x'>= low5 & near_building_dist`x' <high5 & YRBLT`x'>=`s' &  YRBLT`x'<1970 & YRBLT`x'!=. & near_building_type`x'=="main"
		}

	global pre_vars_0to50 pre_SqM_FtPrnt_0_50_*
	global pre_vars_0to100 pre_SqM_FtPrnt_0_100_*
	global pre_vars_0to150 pre_SqM_FtPrnt_0_150_*
	global pre_vars_0to200 pre_SqM_FtPrnt_0_200_*
	global pre_vars_0to250 pre_SqM_FtPrnt_0_250_*

	egen pre_SqM_0to50=rowtotal($pre_vars_0to50)
	egen pre_SqM_0to100=rowtotal($pre_vars_0to100)
	egen pre_SqM_0to150=rowtotal($pre_vars_0to150)
	egen pre_SqM_0to200=rowtotal($pre_vars_0to200)
	egen pre_SqM_0to250=rowtotal($pre_vars_0to250)
	
	gen pre_res_SqM_y`s'=pre_SqM_0to50 if bldg_$d_c ==1
	replace pre_res_SqM_y`s'=pre_SqM_0to100 if bldg_$d_c ==2
	replace pre_res_SqM_y`s'=pre_SqM_0to150 if bldg_$d_c ==3
	replace pre_res_SqM_y`s'=pre_SqM_0to200 if bldg_$d_c ==4
	replace pre_res_SqM_y`s'=pre_SqM_0to250 if bldg_$d_c ==5
	
	drop $pre_vars_0to50 $pre_vars_0to100 $pre_vars_0to150 $pre_vars_0to200 $pre_vars_0to250 pre_SqM_0*
	
	* Square Foot Footprint commercial of Pre-1970 Buildings* WITHIN CIRCLE/DOUGHNUT

	foreach x of numlist 1/413 {
		gen pre_SqM_FtPrnt_0_50_`x'= FootPrint_Area_SqM`x' if near_building_dist`x'>= low1 & near_building_dist`x' <high1 & YRBLT`x'>=`s' &  YRBLT`x'<1970 & YRBLT`x'!=. & near_building_type`x'=="commercial"
		gen pre_SqM_FtPrnt_0_100_`x'= FootPrint_Area_SqM`x' if near_building_dist`x'>= low2 & near_building_dist`x' <high2 & YRBLT`x'>=`s' &  YRBLT`x'<1970 & YRBLT`x'!=. & near_building_type`x'=="commercial"
		gen pre_SqM_FtPrnt_0_150_`x'= FootPrint_Area_SqM`x' if near_building_dist`x'>= low3 & near_building_dist`x' <high3 & YRBLT`x'>=`s' &  YRBLT`x'<1970 & YRBLT`x'!=. & near_building_type`x'=="commercial"
		gen pre_SqM_FtPrnt_0_200_`x'= FootPrint_Area_SqM`x' if near_building_dist`x'>= low4 & near_building_dist`x' <high4 & YRBLT`x'>=`s' &  YRBLT`x'<1970 & YRBLT`x'!=. & near_building_type`x'=="commercial"
		gen pre_SqM_FtPrnt_0_250_`x'= FootPrint_Area_SqM`x' if near_building_dist`x'>= low5 & near_building_dist`x' <high5 & YRBLT`x'>=`s' &  YRBLT`x'<1970 & YRBLT`x'!=. & near_building_type`x'=="commercial"
		}

	global pre_vars_0to50 pre_SqM_FtPrnt_0_50_*
	global pre_vars_0to100 pre_SqM_FtPrnt_0_100_*
	global pre_vars_0to150 pre_SqM_FtPrnt_0_150_*
	global pre_vars_0to200 pre_SqM_FtPrnt_0_200_*
	global pre_vars_0to250 pre_SqM_FtPrnt_0_250_*

	egen pre_SqM_0to50=rowtotal($pre_vars_0to50)
	egen pre_SqM_0to100=rowtotal($pre_vars_0to100)
	egen pre_SqM_0to150=rowtotal($pre_vars_0to150)
	egen pre_SqM_0to200=rowtotal($pre_vars_0to200)
	egen pre_SqM_0to250=rowtotal($pre_vars_0to250)
	
	gen pre_com_SqM_y`s'=pre_SqM_0to50 if bldg_$d_c ==1
	replace pre_com_SqM_y`s'=pre_SqM_0to100 if bldg_$d_c ==2
	replace pre_com_SqM_y`s'=pre_SqM_0to150 if bldg_$d_c ==3
	replace pre_com_SqM_y`s'=pre_SqM_0to200 if bldg_$d_c ==4
	replace pre_com_SqM_y`s'=pre_SqM_0to250 if bldg_$d_c ==5
	
	drop $pre_vars_0to50 $pre_vars_0to100 $pre_vars_0to150 $pre_vars_0to200 $pre_vars_0to250 pre_SqM_0*
	
	* Square Foot Footprint out of Pre-1970 Buildings* WITHIN CIRCLE/DOUGHNUT

	foreach x of numlist 1/413 {
		gen pre_SqM_FtPrnt_0_50_`x'= FootPrint_Area_SqM`x' if near_building_dist`x'>= low1 & near_building_dist`x' <high1 & YRBLT`x'>=`s' &  YRBLT`x'<1970 & YRBLT`x'!=. & near_building_type`x'=="out"
		gen pre_SqM_FtPrnt_0_100_`x'= FootPrint_Area_SqM`x' if near_building_dist`x'>= low2 & near_building_dist`x' <high2 & YRBLT`x'>=`s' &  YRBLT`x'<1970 & YRBLT`x'!=. & near_building_type`x'=="out"
		gen pre_SqM_FtPrnt_0_150_`x'= FootPrint_Area_SqM`x' if near_building_dist`x'>= low3 & near_building_dist`x' <high3 & YRBLT`x'>=`s' &  YRBLT`x'<1970 & YRBLT`x'!=. & near_building_type`x'=="out"
		gen pre_SqM_FtPrnt_0_200_`x'= FootPrint_Area_SqM`x' if near_building_dist`x'>= low4 & near_building_dist`x' <high4 & YRBLT`x'>=`s' &  YRBLT`x'<1970 & YRBLT`x'!=. & near_building_type`x'=="out"
		gen pre_SqM_FtPrnt_0_250_`x'= FootPrint_Area_SqM`x' if near_building_dist`x'>= low5 & near_building_dist`x' <high5 & YRBLT`x'>=`s' &  YRBLT`x'<1970 & YRBLT`x'!=. & near_building_type`x'=="out"
		}

	global pre_vars_0to50 pre_SqM_FtPrnt_0_50_*
	global pre_vars_0to100 pre_SqM_FtPrnt_0_100_*
	global pre_vars_0to150 pre_SqM_FtPrnt_0_150_*
	global pre_vars_0to200 pre_SqM_FtPrnt_0_200_*
	global pre_vars_0to250 pre_SqM_FtPrnt_0_250_*

	egen pre_SqM_0to50=rowtotal($pre_vars_0to50)
	egen pre_SqM_0to100=rowtotal($pre_vars_0to100)
	egen pre_SqM_0to150=rowtotal($pre_vars_0to150)
	egen pre_SqM_0to200=rowtotal($pre_vars_0to200)
	egen pre_SqM_0to250=rowtotal($pre_vars_0to250)
	
	gen pre_out_SqM_y`s'=pre_SqM_0to50 if bldg_$d_c ==1
	replace pre_out_SqM_y`s'=pre_SqM_0to100 if bldg_$d_c ==2
	replace pre_out_SqM_y`s'=pre_SqM_0to150 if bldg_$d_c ==3
	replace pre_out_SqM_y`s'=pre_SqM_0to200 if bldg_$d_c ==4
	replace pre_out_SqM_y`s'=pre_SqM_0to250 if bldg_$d_c ==5
	
	drop $pre_vars_0to50 $pre_vars_0to100 $pre_vars_0to150 $pre_vars_0to200 $pre_vars_0to250 pre_SqM_0*
	}

*****POST VARS*****
foreach n in 2005 2017  {
	
	*Full Count POST 1970 BUILDINGS WITHIN 250 METER	
	
	foreach x of numlist 1/413 {
		foreach y of numlist 250 {
		gen post_build_within_`y'_`x'= (near_building_dist`x'<=`y'& YRBLT`x'>=1970 & YRBLT`x'<=`n')
		}
	}

	global post_vars250 post_build_within_250_*

	egen post_win_250_bld_1970_`n'=rowtotal($post_vars250)
	
	drop $post_vars250
	
	*UNPLUGGED POST 1970 WELLS WITHIN 250 METER	

	foreach x of numlist 1/16 {
		foreach y of numlist 250 {
		gen post_well_within_`y'_`x'=(near_well_dist`x'<=`y' & minyear`x'>=1970 & minyear`x'<=`n' & plugged`x' == 0)
		}
	}

	global post_wvars250 post_well_within_250_*
	

	egen unplug_post_well_win_250_`n'=rowtotal($post_wvars250)
	
	drop $post_wvars250

	*PLUGGED POST 1970 WELLS WITHIN 250 METER	

	foreach x of numlist 1/16 {
		foreach y of numlist 250 {
		gen post_well_within_`y'_`x'=(near_well_dist`x'<=`y' & minyear`x'>=1970 & minyear`x'<=`n' & plugged`x' == 1)
		}
	}

	global post_wvars250 post_well_within_250_*
	

	egen plug_post_well_win_250_`n'=rowtotal($post_wvars250)
	
	drop $post_wvars250
	
	*POST 1970 BUILDINGS WITHIN CIRCLE/DOUGHNUT

	foreach x of numlist 1/413 {
		gen post_build_0_50_`x'= (near_building_dist`x'>= low1 & near_building_dist`x'<high1 & YRBLT`x'>=1970 & YRBLT`x'<=`n')
		gen post_build_0_100_`x'= (near_building_dist`x'>= low2 & near_building_dist`x'<high2 & YRBLT`x'>=1970 & YRBLT`x'<=`n')
		gen post_build_0_150_`x'= (near_building_dist`x'>= low3 & near_building_dist`x'<high3 & YRBLT`x'>=1970 & YRBLT`x'<=`n')
		gen post_build_0_200_`x'= (near_building_dist`x'>= low4 & near_building_dist`x'<high4 & YRBLT`x'>=1970 & YRBLT`x'<=`n')
		gen post_build_0_250_`x'= (near_building_dist`x'>= low5 & near_building_dist`x'<high5 & YRBLT`x'>=1970 & YRBLT`x'<=`n')
		}

	global post_vars_0to50 post_build_0_50_*
	global post_vars_0to100 post_build_0_100_*
	global post_vars_0to150 post_build_0_150_*
	global post_vars_0to200 post_build_0_200_*
	global post_vars_0to250 post_build_0_250_*

	egen post_build_0to50=rowtotal($post_vars_0to50)
	egen post_build_0to100=rowtotal($post_vars_0to100)
	egen post_build_0to150=rowtotal($post_vars_0to150)
	egen post_build_0to200=rowtotal($post_vars_0to200)
	egen post_build_0to250=rowtotal($post_vars_0to250)
	
	gen post_build_`n'=post_build_0to50 if bldg_$d_c ==1 
	replace post_build_`n'=post_build_0to100 if bldg_$d_c ==2
	replace post_build_`n'=post_build_0to150 if bldg_$d_c ==3
	replace post_build_`n'=post_build_0to200 if bldg_$d_c ==4
	replace post_build_`n'=post_build_0to250 if bldg_$d_c ==5
	
	drop $post_vars_0to50 $post_vars_0to100 $post_vars_0to150 $post_vars_0to200 $post_vars_0to250 post_build_0*
	
	*POST 1970 commercial BUILDINGS WITHIN CIRCLE/DOUGHNUT

	foreach x of numlist 1/413 {
		gen post_build_0_50_`x'= (near_building_dist`x'>= low1 & near_building_dist`x'<high1 & YRBLT`x'>=1970 & YRBLT`x'<=`n' & near_building_type`x'=="commercial")
		gen post_build_0_100_`x'= (near_building_dist`x'>= low2 & near_building_dist`x'<high2 & YRBLT`x'>=1970 & YRBLT`x'<=`n'& near_building_type`x'=="commercial")
		gen post_build_0_150_`x'= (near_building_dist`x'>= low3 & near_building_dist`x'<high3 & YRBLT`x'>=1970 & YRBLT`x'<=`n'& near_building_type`x'=="commercial")
		gen post_build_0_200_`x'= (near_building_dist`x'>= low4 & near_building_dist`x'<high4 & YRBLT`x'>=1970 & YRBLT`x'<=`n'& near_building_type`x'=="commercial")
		gen post_build_0_250_`x'= (near_building_dist`x'>= low5 & near_building_dist`x'<high5 & YRBLT`x'>=1970 & YRBLT`x'<=`n'& near_building_type`x'=="commercial")
		}

	global post_vars_0to50 post_build_0_50_*
	global post_vars_0to100 post_build_0_100_*
	global post_vars_0to150 post_build_0_150_*
	global post_vars_0to200 post_build_0_200_*
	global post_vars_0to250 post_build_0_250_*

	egen post_build_0to50=rowtotal($post_vars_0to50)
	egen post_build_0to100=rowtotal($post_vars_0to100)
	egen post_build_0to150=rowtotal($post_vars_0to150)
	egen post_build_0to200=rowtotal($post_vars_0to200)
	egen post_build_0to250=rowtotal($post_vars_0to250)
	
	gen post_com_build_`n'=post_build_0to50 if bldg_$d_c ==1 
	replace post_com_build_`n'=post_build_0to100 if bldg_$d_c ==2
	replace post_com_build_`n'=post_build_0to150 if bldg_$d_c ==3
	replace post_com_build_`n'=post_build_0to200 if bldg_$d_c ==4
	replace post_com_build_`n'=post_build_0to250 if bldg_$d_c ==5
	
	drop $post_vars_0to50 $post_vars_0to100 $post_vars_0to150 $post_vars_0to200 $post_vars_0to250 post_build_0*
	
	*POST 1970 out BUILDINGS WITHIN CIRCLE/DOUGHNUT

	foreach x of numlist 1/413 {
		gen post_build_0_50_`x'= (near_building_dist`x'>= low1 & near_building_dist`x'<high1 & YRBLT`x'>=1970 & YRBLT`x'<=`n' & near_building_type`x'=="out")
		gen post_build_0_100_`x'= (near_building_dist`x'>= low2 & near_building_dist`x'<high2 & YRBLT`x'>=1970 & YRBLT`x'<=`n'& near_building_type`x'=="out")
		gen post_build_0_150_`x'= (near_building_dist`x'>= low3 & near_building_dist`x'<high3 & YRBLT`x'>=1970 & YRBLT`x'<=`n'& near_building_type`x'=="out")
		gen post_build_0_200_`x'= (near_building_dist`x'>= low4 & near_building_dist`x'<high4 & YRBLT`x'>=1970 & YRBLT`x'<=`n'& near_building_type`x'=="out")
		gen post_build_0_250_`x'= (near_building_dist`x'>= low5 & near_building_dist`x'<high5 & YRBLT`x'>=1970 & YRBLT`x'<=`n'& near_building_type`x'=="out")
		}

	global post_vars_0to50 post_build_0_50_*
	global post_vars_0to100 post_build_0_100_*
	global post_vars_0to150 post_build_0_150_*
	global post_vars_0to200 post_build_0_200_*
	global post_vars_0to250 post_build_0_250_*

	egen post_build_0to50=rowtotal($post_vars_0to50)
	egen post_build_0to100=rowtotal($post_vars_0to100)
	egen post_build_0to150=rowtotal($post_vars_0to150)
	egen post_build_0to200=rowtotal($post_vars_0to200)
	egen post_build_0to250=rowtotal($post_vars_0to250)
	
	gen post_out_build_`n'=post_build_0to50 if bldg_$d_c ==1 
	replace post_out_build_`n'=post_build_0to100 if bldg_$d_c ==2
	replace post_out_build_`n'=post_build_0to150 if bldg_$d_c ==3
	replace post_out_build_`n'=post_build_0to200 if bldg_$d_c ==4
	replace post_out_build_`n'=post_build_0to250 if bldg_$d_c ==5
	
	drop $post_vars_0to50 $post_vars_0to100 $post_vars_0to150 $post_vars_0to200 $post_vars_0to250 post_build_0*
	
	*POST 1970 residential BUILDINGS WITHIN CIRCLE/DOUGHNUT

	foreach x of numlist 1/413 {
		gen post_build_0_50_`x'= (near_building_dist`x'>= low1 & near_building_dist`x'<high1 & YRBLT`x'>=1970 & YRBLT`x'<=`n' & near_building_type`x'=="main")
		gen post_build_0_100_`x'= (near_building_dist`x'>= low2 & near_building_dist`x'<high2 & YRBLT`x'>=1970 & YRBLT`x'<=`n'& near_building_type`x'=="main")
		gen post_build_0_150_`x'= (near_building_dist`x'>= low3 & near_building_dist`x'<high3 & YRBLT`x'>=1970 & YRBLT`x'<=`n'& near_building_type`x'=="main")
		gen post_build_0_200_`x'= (near_building_dist`x'>= low4 & near_building_dist`x'<high4 & YRBLT`x'>=1970 & YRBLT`x'<=`n'& near_building_type`x'=="main")
		gen post_build_0_250_`x'= (near_building_dist`x'>= low5 & near_building_dist`x'<high5 & YRBLT`x'>=1970 & YRBLT`x'<=`n'& near_building_type`x'=="main")
		}

	global post_vars_0to50 post_build_0_50_*
	global post_vars_0to100 post_build_0_100_*
	global post_vars_0to150 post_build_0_150_*
	global post_vars_0to200 post_build_0_200_*
	global post_vars_0to250 post_build_0_250_*

	egen post_build_0to50=rowtotal($post_vars_0to50)
	egen post_build_0to100=rowtotal($post_vars_0to100)
	egen post_build_0to150=rowtotal($post_vars_0to150)
	egen post_build_0to200=rowtotal($post_vars_0to200)
	egen post_build_0to250=rowtotal($post_vars_0to250)
	
	gen post_res_build_`n'=post_build_0to50 if bldg_$d_c ==1 
	replace post_res_build_`n'=post_build_0to100 if bldg_$d_c ==2
	replace post_res_build_`n'=post_build_0to150 if bldg_$d_c ==3
	replace post_res_build_`n'=post_build_0to200 if bldg_$d_c ==4
	replace post_res_build_`n'=post_build_0to250 if bldg_$d_c ==5
	
	drop $post_vars_0to50 $post_vars_0to100 $post_vars_0to150 $post_vars_0to200 $post_vars_0to250 post_build_0*
	
	* Total Rooms in Post-1970 Residential Buildings* WITHIN CIRCLE/DOUGHNUT

	foreach x of numlist 1/413 {
		gen post_RMTOT_0_50_`x'= RMTOT`x' if near_building_dist`x'>= low1 & near_building_dist`x'<high1 &  YRBLT`x'>=1970 & YRBLT`x'<=`n'
		gen post_RMTOT_0_100_`x'= RMTOT`x' if near_building_dist`x'>= low2 & near_building_dist`x'<high2 &  YRBLT`x'>=1970 & YRBLT`x'<=`n'
		gen post_RMTOT_0_150_`x'= RMTOT`x' if near_building_dist`x'>= low3 & near_building_dist`x'<high3 &  YRBLT`x'>=1970 & YRBLT`x'<=`n'
		gen post_RMTOT_0_200_`x'= RMTOT`x' if near_building_dist`x'>= low4 & near_building_dist`x'<high4 &  YRBLT`x'>=1970 & YRBLT`x'<=`n'
		gen post_RMTOT_0_250_`x'= RMTOT`x' if near_building_dist`x'>= low5 & near_building_dist`x'<high5 &  YRBLT`x'>=1970 & YRBLT`x'<=`n'
		}

	global post_vars_0to50 post_RMTOT_0_50_*
	global post_vars_0to100 post_RMTOT_0_100_*
	global post_vars_0to150 post_RMTOT_0_150_*
	global post_vars_0to200 post_RMTOT_0_200_*
	global post_vars_0to250 post_RMTOT_0_250_*

	egen post_RMTOT_0to50=rowtotal($post_vars_0to50)
	egen post_RMTOT_0to100=rowtotal($post_vars_0to100)
	egen post_RMTOT_0to150=rowtotal($post_vars_0to150)
	egen post_RMTOT_0to200=rowtotal($post_vars_0to200)
	egen post_RMTOT_0to250=rowtotal($post_vars_0to250)
	
	gen post_RMTOT_`n'=post_RMTOT_0to50 if bldg_$d_c ==1
	replace post_RMTOT_`n'=post_RMTOT_0to100 if bldg_$d_c ==2
	replace post_RMTOT_`n'=post_RMTOT_0to150 if bldg_$d_c ==3
	replace post_RMTOT_`n'=post_RMTOT_0to200 if bldg_$d_c ==4
	replace post_RMTOT_`n'=post_RMTOT_0to250 if bldg_$d_c ==5
	
	drop $post_vars_0to50 $post_vars_0to100 $post_vars_0to150 $post_vars_0to200 $post_vars_0to250 post_RMTOT_0*
	
	* Total SFLA in Post-1970 Residential Buildings* WITHIN CIRCLE/DOUGHNUT
	foreach x of numlist 1/413 {
		gen post_SFLA_0_50_`x'= SFLA`x' if near_building_dist`x'>= low1 & near_building_dist`x'<high1 &  YRBLT`x'>=1970 & YRBLT`x'<=`n'
		gen post_SFLA_0_100_`x'= SFLA`x' if near_building_dist`x'>= low2 & near_building_dist`x'<high2 &  YRBLT`x'>=1970 & YRBLT`x'<=`n'
		gen post_SFLA_0_150_`x'= SFLA`x' if near_building_dist`x'>= low3 & near_building_dist`x'<high3 &  YRBLT`x'>=1970 & YRBLT`x'<=`n'
		gen post_SFLA_0_200_`x'= SFLA`x' if near_building_dist`x'>= low4 & near_building_dist`x'<high4 &  YRBLT`x'>=1970 & YRBLT`x'<=`n'
		gen post_SFLA_0_250_`x'= SFLA`x' if near_building_dist`x'>= low5 & near_building_dist`x'<high5 &  YRBLT`x'>=1970 & YRBLT`x'<=`n'
		}

	global post_vars_0to50 post_SFLA_0_50_*
	global post_vars_0to100 post_SFLA_0_100_*
	global post_vars_0to150 post_SFLA_0_150_*
	global post_vars_0to200 post_SFLA_0_200_*
	global post_vars_0to250 post_SFLA_0_250_*

	egen post_SFLA_0to50=rowtotal($post_vars_0to50)
	egen post_SFLA_0to100=rowtotal($post_vars_0to100)
	egen post_SFLA_0to150=rowtotal($post_vars_0to150)
	egen post_SFLA_0to200=rowtotal($post_vars_0to200)
	egen post_SFLA_0to250=rowtotal($post_vars_0to250)
	
	gen post_SFLA_`n'=post_SFLA_0to50 if bldg_$d_c ==1
	replace post_SFLA_`n'=post_SFLA_0to100 if bldg_$d_c ==2
	replace post_SFLA_`n'=post_SFLA_0to150 if bldg_$d_c ==3
	replace post_SFLA_`n'=post_SFLA_0to200 if bldg_$d_c ==4
	replace post_SFLA_`n'=post_SFLA_0to250 if bldg_$d_c ==5
	
	drop $post_vars_0to50 $post_vars_0to100 $post_vars_0to150 $post_vars_0to200 $post_vars_0to250 post_SFLA_0*
	
	* Square Foot Footprint of POST 1970 Buildings* WITHIN CIRCLE/DOUGHNUT

	foreach x of numlist 1/413 {
		gen post_SqM_FtPrnt_0_50_`x'= FootPrint_Area_SqM`x' if near_building_dist`x'>= low1 & near_building_dist`x'<high1 &  YRBLT`x'>=1970 & YRBLT`x'<=`n'
		gen post_SqM_FtPrnt_0_100_`x'= FootPrint_Area_SqM`x' if near_building_dist`x'>= low2 & near_building_dist`x'<high2 &  YRBLT`x'>=1970 & YRBLT`x'<=`n'
		gen post_SqM_FtPrnt_0_150_`x'= FootPrint_Area_SqM`x' if near_building_dist`x'>= low3 & near_building_dist`x'<high3 &  YRBLT`x'>=1970 & YRBLT`x'<=`n'
		gen post_SqM_FtPrnt_0_200_`x'= FootPrint_Area_SqM`x' if near_building_dist`x'>= low4 & near_building_dist`x'<high4 &  YRBLT`x'>=1970 & YRBLT`x'<=`n'
		gen post_SqM_FtPrnt_0_250_`x'= FootPrint_Area_SqM`x' if near_building_dist`x'>= low5 & near_building_dist`x'<high5 &  YRBLT`x'>=1970 & YRBLT`x'<=`n'
		}

	global post_vars_0to50 post_SqM_FtPrnt_0_50_*
	global post_vars_0to100 post_SqM_FtPrnt_0_100_*
	global post_vars_0to150 post_SqM_FtPrnt_0_150_*
	global post_vars_0to200 post_SqM_FtPrnt_0_200_*
	global post_vars_0to250 post_SqM_FtPrnt_0_250_*

	egen post_SqM_0to50=rowtotal($post_vars_0to50)
	egen post_SqM_0to100=rowtotal($post_vars_0to100)
	egen post_SqM_0to150=rowtotal($post_vars_0to150)
	egen post_SqM_0to200=rowtotal($post_vars_0to200)
	egen post_SqM_0to250=rowtotal($post_vars_0to250)
	
	gen post_SqM_`n'=post_SqM_0to50 if bldg_$d_c ==1
	replace post_SqM_`n'=post_SqM_0to100 if bldg_$d_c ==2
	replace post_SqM_`n'=post_SqM_0to150 if bldg_$d_c ==3
	replace post_SqM_`n'=post_SqM_0to200 if bldg_$d_c ==4
	replace post_SqM_`n'=post_SqM_0to250 if bldg_$d_c ==5
	
	drop $post_vars_0to50 $post_vars_0to100 $post_vars_0to150 $post_vars_0to200 $post_vars_0to250 post_SqM_0*
	
	* Square Foot Footprint residential of POST 1970 Buildings* WITHIN CIRCLE/DOUGHNUT

	foreach x of numlist 1/413 {
		gen post_SqM_FtPrnt_0_50_`x'= FootPrint_Area_SqM`x' if near_building_dist`x'>= low1 & near_building_dist`x'<high1 &  YRBLT`x'>=1970 & YRBLT`x'<=`n' & near_building_type`x'=="main"
		gen post_SqM_FtPrnt_0_100_`x'= FootPrint_Area_SqM`x' if near_building_dist`x'>= low2 & near_building_dist`x'<high2 &  YRBLT`x'>=1970 & YRBLT`x'<=`n' & near_building_type`x'=="main"
		gen post_SqM_FtPrnt_0_150_`x'= FootPrint_Area_SqM`x' if near_building_dist`x'>= low3 & near_building_dist`x'<high3 &  YRBLT`x'>=1970 & YRBLT`x'<=`n' & near_building_type`x'=="main"
		gen post_SqM_FtPrnt_0_200_`x'= FootPrint_Area_SqM`x' if near_building_dist`x'>= low4 & near_building_dist`x'<high4 &  YRBLT`x'>=1970 & YRBLT`x'<=`n' & near_building_type`x'=="main"
		gen post_SqM_FtPrnt_0_250_`x'= FootPrint_Area_SqM`x' if near_building_dist`x'>= low5 & near_building_dist`x'<high5 &  YRBLT`x'>=1970 & YRBLT`x'<=`n' & near_building_type`x'=="main"
		}

	global post_vars_0to50 post_SqM_FtPrnt_0_50_*
	global post_vars_0to100 post_SqM_FtPrnt_0_100_*
	global post_vars_0to150 post_SqM_FtPrnt_0_150_*
	global post_vars_0to200 post_SqM_FtPrnt_0_200_*
	global post_vars_0to250 post_SqM_FtPrnt_0_250_*

	egen post_SqM_0to50=rowtotal($post_vars_0to50)
	egen post_SqM_0to100=rowtotal($post_vars_0to100)
	egen post_SqM_0to150=rowtotal($post_vars_0to150)
	egen post_SqM_0to200=rowtotal($post_vars_0to200)
	egen post_SqM_0to250=rowtotal($post_vars_0to250)
	
	gen post_res_SqM_`n'=post_SqM_0to50 if bldg_$d_c ==1
	replace post_res_SqM_`n'=post_SqM_0to100 if bldg_$d_c ==2
	replace post_res_SqM_`n'=post_SqM_0to150 if bldg_$d_c ==3
	replace post_res_SqM_`n'=post_SqM_0to200 if bldg_$d_c ==4
	replace post_res_SqM_`n'=post_SqM_0to250 if bldg_$d_c ==5
	
	drop $post_vars_0to50 $post_vars_0to100 $post_vars_0to150 $post_vars_0to200 $post_vars_0to250 post_SqM_0*
	
	* Square Foot Footprint commercial of POST 1970 Buildings* WITHIN CIRCLE/DOUGHNUT

	foreach x of numlist 1/413 {
		gen post_SqM_FtPrnt_0_50_`x'= FootPrint_Area_SqM`x' if near_building_dist`x'>= low1 & near_building_dist`x'<high1 &  YRBLT`x'>=1970 & YRBLT`x'<=`n' & near_building_type`x'=="commercial"
		gen post_SqM_FtPrnt_0_100_`x'= FootPrint_Area_SqM`x' if near_building_dist`x'>= low2 & near_building_dist`x'<high2 &  YRBLT`x'>=1970 & YRBLT`x'<=`n' & near_building_type`x'=="commercial"
		gen post_SqM_FtPrnt_0_150_`x'= FootPrint_Area_SqM`x' if near_building_dist`x'>= low3 & near_building_dist`x'<high3 &  YRBLT`x'>=1970 & YRBLT`x'<=`n' & near_building_type`x'=="commercial"
		gen post_SqM_FtPrnt_0_200_`x'= FootPrint_Area_SqM`x' if near_building_dist`x'>= low4 & near_building_dist`x'<high4 &  YRBLT`x'>=1970 & YRBLT`x'<=`n' & near_building_type`x'=="commercial"
		gen post_SqM_FtPrnt_0_250_`x'= FootPrint_Area_SqM`x' if near_building_dist`x'>= low5 & near_building_dist`x'<high5 &  YRBLT`x'>=1970 & YRBLT`x'<=`n' & near_building_type`x'=="commercial"
		}

	global post_vars_0to50 post_SqM_FtPrnt_0_50_*
	global post_vars_0to100 post_SqM_FtPrnt_0_100_*
	global post_vars_0to150 post_SqM_FtPrnt_0_150_*
	global post_vars_0to200 post_SqM_FtPrnt_0_200_*
	global post_vars_0to250 post_SqM_FtPrnt_0_250_*

	egen post_SqM_0to50=rowtotal($post_vars_0to50)
	egen post_SqM_0to100=rowtotal($post_vars_0to100)
	egen post_SqM_0to150=rowtotal($post_vars_0to150)
	egen post_SqM_0to200=rowtotal($post_vars_0to200)
	egen post_SqM_0to250=rowtotal($post_vars_0to250)
	
	gen post_com_SqM_`n'=post_SqM_0to50 if bldg_$d_c ==1
	replace post_com_SqM_`n'=post_SqM_0to100 if bldg_$d_c ==2
	replace post_com_SqM_`n'=post_SqM_0to150 if bldg_$d_c ==3
	replace post_com_SqM_`n'=post_SqM_0to200 if bldg_$d_c ==4
	replace post_com_SqM_`n'=post_SqM_0to250 if bldg_$d_c ==5
	
	drop $post_vars_0to50 $post_vars_0to100 $post_vars_0to150 $post_vars_0to200 $post_vars_0to250 post_SqM_0*
	
	* Square Foot Footprint of out POST 1970 Buildings* WITHIN CIRCLE/DOUGHNUT

	foreach x of numlist 1/413 {
		gen post_SqM_FtPrnt_0_50_`x'= FootPrint_Area_SqM`x' if near_building_dist`x'>= low1 & near_building_dist`x'<high1 &  YRBLT`x'>=1970 & YRBLT`x'<=`n' & near_building_type`x'=="out"
		gen post_SqM_FtPrnt_0_100_`x'= FootPrint_Area_SqM`x' if near_building_dist`x'>= low2 & near_building_dist`x'<high2 &  YRBLT`x'>=1970 & YRBLT`x'<=`n' & near_building_type`x'=="out"
		gen post_SqM_FtPrnt_0_150_`x'= FootPrint_Area_SqM`x' if near_building_dist`x'>= low3 & near_building_dist`x'<high3 &  YRBLT`x'>=1970 & YRBLT`x'<=`n' & near_building_type`x'=="out" 
		gen post_SqM_FtPrnt_0_200_`x'= FootPrint_Area_SqM`x' if near_building_dist`x'>= low4 & near_building_dist`x'<high4 &  YRBLT`x'>=1970 & YRBLT`x'<=`n' & near_building_type`x'=="out"
		gen post_SqM_FtPrnt_0_250_`x'= FootPrint_Area_SqM`x' if near_building_dist`x'>= low5 & near_building_dist`x'<high5 &  YRBLT`x'>=1970 & YRBLT`x'<=`n' & near_building_type`x'=="out"
		}

	global post_vars_0to50 post_SqM_FtPrnt_0_50_*
	global post_vars_0to100 post_SqM_FtPrnt_0_100_*
	global post_vars_0to150 post_SqM_FtPrnt_0_150_*
	global post_vars_0to200 post_SqM_FtPrnt_0_200_*
	global post_vars_0to250 post_SqM_FtPrnt_0_250_*

	egen post_SqM_0to50=rowtotal($post_vars_0to50)
	egen post_SqM_0to100=rowtotal($post_vars_0to100)
	egen post_SqM_0to150=rowtotal($post_vars_0to150)
	egen post_SqM_0to200=rowtotal($post_vars_0to200)
	egen post_SqM_0to250=rowtotal($post_vars_0to250)
	
	gen post_out_SqM_`n'=post_SqM_0to50 if bldg_$d_c ==1
	replace post_out_SqM_`n'=post_SqM_0to100 if bldg_$d_c ==2
	replace post_out_SqM_`n'=post_SqM_0to150 if bldg_$d_c ==3
	replace post_out_SqM_`n'=post_SqM_0to200 if bldg_$d_c ==4
	replace post_out_SqM_`n'=post_SqM_0to250 if bldg_$d_c ==5
	
	drop $post_vars_0to50 $post_vars_0to100 $post_vars_0to150 $post_vars_0to200 $post_vars_0to250 post_SqM_0*
	}
	
*POST LOTS WITHIN CIRCLE/DOUGHNUT

	foreach x of numlist 1/412 {
		gen post_lot_0_50_`x'= (near_lot_dist`x'>= low1 & near_lot_dist`x'<high1)
		gen post_lot_0_100_`x'= (near_lot_dist`x'>= low2 & near_lot_dist`x'<high2)
		gen post_lot_0_150_`x'= (near_lot_dist`x'>= low3 & near_lot_dist`x'<high3)
		gen post_lot_0_200_`x'= (near_lot_dist`x'>= low3  & near_lot_dist`x'<high4)
		gen post_lot_0_250_`x'= (near_lot_dist`x'>= low4 & near_lot_dist`x'<high5)
		}

	global post_vars_0to50 post_lot_0_50_*
	global post_vars_0to100 post_lot_0_100_*
	global post_vars_0to150 post_lot_0_150_*
	global post_vars_0to200 post_lot_0_200_*
	global post_vars_0to250 post_lot_0_250_*

	egen post_lot_0to50=rowtotal($post_vars_0to50)
	egen post_lot_0to100=rowtotal($post_vars_0to100)
	egen post_lot_0to150=rowtotal($post_vars_0to150)
	egen post_lot_0to200=rowtotal($post_vars_0to200)
	egen post_lot_0to250=rowtotal($post_vars_0to250)
	
	gen post_lot=post_lot_0to50 if bldg_$d_c ==1
	replace post_lot=post_lot_0to100 if bldg_$d_c ==2
	replace post_lot=post_lot_0to150 if bldg_$d_c ==3
	replace post_lot=post_lot_0to200 if bldg_$d_c ==4
	replace post_lot=post_lot_0to250 if bldg_$d_c ==5
	
	drop $post_vars_0to50 $post_vars_0to100 $post_vars_0to150 $post_vars_0to200 $post_vars_0to250 post_lot_*
	
*POST PARCELS WITHIN CIRCLE/DOUGHNUT

	foreach x of numlist 1/407 {
		gen post_parcel_0_50_`x'= (near_parcel_dist`x'>= low1 & near_parcel_dist`x'<high1)
		gen post_parcel_0_100_`x'= (near_parcel_dist`x'>= low2 & near_parcel_dist`x'<high2)
		gen post_parcel_0_150_`x'= (near_parcel_dist`x'>= low3 & near_parcel_dist`x'<high3)
		gen post_parcel_0_200_`x'= (near_parcel_dist`x'>= low4 & near_parcel_dist`x'<high4)
		gen post_parcel_0_250_`x'= (near_parcel_dist`x'>= low5 & near_parcel_dist`x'<high5)
		}

	global post_vars_0to50 post_parcel_0_50_*
	global post_vars_0to100 post_parcel_0_100_*
	global post_vars_0to150 post_parcel_0_150_*
	global post_vars_0to200 post_parcel_0_200_*
	global post_vars_0to250 post_parcel_0_250_*

	egen post_parcel_0to50=rowtotal($post_vars_0to50)
	egen post_parcel_0to100=rowtotal($post_vars_0to100)
	egen post_parcel_0to150=rowtotal($post_vars_0to150)
	egen post_parcel_0to200=rowtotal($post_vars_0to200)
	egen post_parcel_0to250=rowtotal($post_vars_0to250)
	
	gen post_parcel=post_parcel_0to50 if bldg_$d_c ==1
	replace post_parcel=post_parcel_0to100 if bldg_$d_c ==2
	replace post_parcel=post_parcel_0to150 if bldg_$d_c ==3
	replace post_parcel=post_parcel_0to200 if bldg_$d_c ==4
	replace post_parcel=post_parcel_0to250 if bldg_$d_c ==5
	
	drop $post_vars_0to50 $post_vars_0to100 $post_vars_0to150 $post_vars_0to200 $post_vars_0to250 post_parcel_*
	
*DROP USELESS VARIABLES
	
	rename minyear these
	rename welltype name
	rename never_report changes
	rename permit_num help
	drop OBJECTID permit_num* Join_Count TARGET_FID latitudede longituded near_building_dist* near_building_type* YRBLT* SFLA* RMTOT* RMBED* FIXBATH* central_ac* FootPrint_Area_SqM* near_well_dist* welltype* plugged* never_report* minyear* near_parcel_dist* near_lot_dist* latitudedec longitudedec event
	rename these minyear
	rename name welltype
	rename changes never_report
	rename help permit_num
	
save Table_$d_c$low_high1$low_high2.dta, replace
